package gewu.suleju.controller;

import gewu.suleju.model.pojo.HouseInfor;
import gewu.suleju.model.pojo.RenterReservation;
import gewu.suleju.model.vo.ReservationInfo;
import gewu.suleju.model.vo.Result;
import gewu.suleju.sevice.imp.HouseInforServiceImp;
import gewu.suleju.sevice.imp.RenterReservationServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RequestMapping("reservat")
@RestController
public class RenterReservationController {
    @Autowired
    private RenterReservationServiceImp renterReservationServiceImp;
    @Autowired
    private HouseInforServiceImp houseInforServiceImp;
    @Autowired
    private ReservationInfo reservationInfo;
    @RequestMapping("renter")
    public Result renter(String data){
        ArrayList<RenterReservation> rr = new ArrayList<>();
        ArrayList<HouseInfor> hi = new ArrayList<>();
        String str = data;
        String[] str1 = str.split("&");
        for(int i=1;i<str1.length;i++){
            String[] str2 = str1[i].split("!");
            RenterReservation renterReservation= renterReservationServiceImp.findByUserIdAndHouseInforId(Integer.parseInt(str2[2]),Integer.parseInt(str2[0]));
            rr.add(renterReservation);
            HouseInfor houseInfor = houseInforServiceImp.findById(Integer.parseInt(str2[0]));
            hi.add(houseInfor);
        }
        reservationInfo.setHouseInfor(hi);
        reservationInfo.setRess(rr);
        return new Result("200","",reservationInfo);
    }
    @RequestMapping("haveReadButton")
    public Result haveReadButton(){
        ArrayList<RenterReservation> rr = renterReservationServiceImp.findByIsStatus(0);
        reservationInfo.setRess(rr);
        ArrayList<HouseInfor> hi = new ArrayList<>();
        for(int i = 0;i < rr.size();i++){
            hi.add(houseInforServiceImp.findById(rr.get(i).getHouseInforId()));
        }
        reservationInfo.setHouseInfor(hi);
        System.out.println(reservationInfo);
        return new Result("200","",reservationInfo);
    }
    @RequestMapping("noReadButton")
    public Result noReadButton(){
        ArrayList<RenterReservation> rr = renterReservationServiceImp.findByIsStatus(1);
        reservationInfo.setRess(rr);
        ArrayList<HouseInfor> hi = new ArrayList<>();
        for(int i = 0;i < rr.size();i++){
            hi.add(houseInforServiceImp.findById(rr.get(i).getHouseInforId()));
        }
        reservationInfo.setHouseInfor(hi);
        return new Result("200","",reservationInfo);
    }
    @RequestMapping("setHaveRead")
    public Result setHaveRead(String id){
        renterReservationServiceImp.updateStatus(Integer.parseInt(id));
        return new Result("200","已读");
    }
    @RequestMapping("setNoRead")
    public Result setNoRead(String id){
        renterReservationServiceImp.updateStatus1(Integer.parseInt(id));
        return new Result("200","已读");
    }
}
